#!/usr/bin/perl

$tmp=$ARGV[0];
chomp($tmp);
open(LWG,$tmp);

$tmp=$ARGV[1];
chomp($tmp);
open(MAP_eng,$tmp);

$tmp=$ARGV[2];
chomp($tmp);
open(MAP_hnd,$tmp);

@lwgs = <LWG>;
@maps_eng = <MAP_eng>;
@maps_hnd = <MAP_hnd>;

$no_lwg = @lwgs;
$no_map_eng = @maps_eng;
$no_map_hnd = @maps_hnd;

%hash_map_eng = ();
%hash_map_hnd = ();

for ($i = 0; $i < $no_map_eng; $i++)
{
	($senid,$src_word,$tgt_word) = split(/\s+/,$maps_eng[$i]);
	$src_word--;
	$tgt_word--;
	$hash_map_eng{ $senid }{ $tgt_word } = $hash_map_eng{ $senid }{ $tgt_word }.$src_word."_";
}


for ($i = 0; $i < $no_map_hnd; $i++)
{
	($senid,$src_word,$tgt_word) = split(/\s+/,$maps_hnd[$i]);
	$src_word--;
	$tgt_word--;
	$hash_map_hnd{ $senid }{ $tgt_word } = $hash_map_hnd{ $senid }{ $tgt_word }.$src_word."_";
}

foreach $line1 (@lwgs)
{
	my ($senid, $src_al, $tgt_al) = split(/\s+/,$line1);

	$main_eng = $hash_map_eng{ $senid }{ $src_al };
	chop($main_eng);

	$main_hnd = $hash_map_hnd{ $senid }{ $tgt_al };
	chop($main_hnd);

	@srcs = split(/_/,$main_eng);
	@tgts = split(/_/,$main_hnd);


	foreach $src (@srcs)
	{
		foreach $tgt (@tgts)
		{
			print $senid,"\t",$src,"\t",$tgt,"\n";
		}
	}
}
